package com.whxy02.tolearn.app.utils;

import com.whxy02.tolearn.app.beans.Answer;
import com.whxy02.tolearn.app.beans.Comment;
import com.whxy02.tolearn.app.beans.Content;
import com.whxy02.tolearn.app.beans.Question;
import com.whxy02.tolearn.app.service.AnswerService;
import com.whxy02.tolearn.app.service.CommentService;
import com.whxy02.tolearn.app.service.ContentService;
import com.whxy02.tolearn.app.service.QuestionService;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class Split {

    @Resource
    ContentService cs;

    @Resource
    CommentService coms;

    @Resource
    AnswerService as;

    public String[] getContent(Integer questionId) throws Exception {
        Content content=cs.getByQuestionId(questionId);
//        String str=content.getContent().replace(" ","").replace("\n","").replace("\r","");
        if(content!=null){
            return content.getContent().split("@@");
        }
        return null;

    }

    public List<Integer> getQuestionId(String questionId){
        List<Integer> list=new ArrayList<>();
        String str=questionId.replace(" ","").replace("\n","").replace("\r","");
        for(String s:str.split("@@")){
            list.add(Integer.parseInt(s));
        }
        return list;
    }

    public String[] getAnswer(Integer questionId) throws Exception {
//        answer=answer.replace(" ","").replace("\n","").replace("\r","");
        Answer a=as.getByQuestionId(questionId);
        if(a!=null) {
            String answer = a.getAnswer();
            return answer.split("@@");
        }
        return null;

    }

    public String[] splitPaper(String paper){
//        paper=paper.replace(" ","").replace("\n","").replace("\r","");
        return paper.split("@@@");
    }

    public List<Comment> getComment(Question question) throws Exception {
        String str=question.getCommentId().replace(" ","").replace("\n","").replace("\r","");
        List<Integer> ids=new ArrayList<>();
        for(String s:str.split("@@")){
            ids.add(Integer.parseInt(s));
        }
        return coms.getByIds(ids);
    }
}
